package com.education.websocket.data;

import com.education.welco.pojo.MessageNotification;
import com.education.welco.service.IMessageNotificationService;
import com.education.welco.service.ISCatalogService;
import com.education.welco.service.ISCatalogWorkService;
import com.education.welco.service.OrderPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: liftsail
 * @Date: 2022/10/15 15:10
 * @Description: 不积跬步无以至千里
 */
@Component
public class HomeBuffer {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ISCatalogWorkService catalogWorkService;

    @Autowired
    private OrderPaperService orderPaperService;

    @Autowired
    private IMessageNotificationService messageNotificationService;

    @Autowired
    private ISCatalogService isCatalogService;

    //首页数据websocket初始化
    public Object data(String teaId) {
        //待批改作业初始化作业  key -> wait:data:teaId
        Object res = redisTemplate.opsForValue().get("wait:data:" + teaId);
        if (Objects.nonNull(res)) {
            return res;
        }
        //初始化数据
        HomeData homeData = new HomeData();
        //查询老师作业待批改数量
        int workWait = catalogWorkService.selectWaitStudentWorkNumByTeaId(teaId);
        //查询老师试卷待批改数量
        int paperWait = orderPaperService.selectWaitStudentPaperNumByTeaId(teaId);
        //查询老师信息待阅读数量
        MessageNotification messageNotification = new MessageNotification();
        messageNotification.setTeaId(Integer.valueOf(teaId));
        messageNotification.setIsRead(false);
        List<MessageNotification> messageNotifications = messageNotificationService.selectMessageNotificationList(messageNotification);
        int mesSize = messageNotifications.size();
        //查询首页-我的课堂信息
        /*Map<String, List<ScatalogClassVO>> sdss = new HashMap<>();
        List<String> weekDay = getWeekDay();
        for (String today : weekDay) {
            List<ScatalogClassVO> sds = isCatalogService.findClassScheduleByTeaId(Integer.valueOf(teaId), today, null);
            //开课时间格式处理  08:00    SimpleDateFormat sdf = new SimpleDateFormat("HH-mm");

            //关于两个字段判断为一个字段的逻辑处理
            //isLz 直播课 还是普通课  （1：开启录制，0不开启录制）
            //status 直播课的状态  0 未开始  1直播中 2录播结束
            //finalStatus:专业课0 未直播1 直播中2 直播结束(录播)3
            for (ScatalogClassVO scatalogClassVO : sds) {
                //处理时间格式
                //第一层判断 (直播课 还是普通课)
                if (scatalogClassVO.getIsLz().equals("1")) {
                    //第二层判断
                    switch (scatalogClassVO.getStatus()) {
                        case "0":
                            scatalogClassVO.setFinalStatus("1");
                            break;
                        case "1":
                            scatalogClassVO.setFinalStatus("2");
                            break;
                        case "2":
                            scatalogClassVO.setFinalStatus("3");
                            break;
                    }
                } else {
                    //设置专业课状态
                    scatalogClassVO.setFinalStatus("0");
                }
            }
            sdss.put(today, sds);
        }*/

        homeData.setWorkWait(workWait);
        homeData.setPaperWait(paperWait);
        homeData.setMessageWait(mesSize);
        redisTemplate.opsForValue().set("wait:data:" + teaId, homeData);
        return homeData;
    }

    //获取本周七天
    public static List<String> getWeekDay() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DAY_OF_WEEK, -1);
        }
        Date[] dates = new Date[7];  // new Date[7] 星期日
        for (int i = 0; i < 7; i++) {  // i < 7 星期日
            dates[i] = calendar.getTime();
            calendar.add(Calendar.DATE, 1);
        }
        List<String> dateStrList = new ArrayList<>();
        for (Date date : dates) {
            dateStrList.add(dateFormat.format(date));
        }
        return dateStrList;
    }

}
